package com.xudm.imp.controller;


import cn.hutool.json.JSONObject;
import com.xudm.imp.service.MoonShotApi;
import org.apache.hc.core5.http.ParseException;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

@RestController
@RequestMapping("/moonshot")
public class MoonShotController {
    private final MoonShotApi moonShotApi;

    public MoonShotController(MoonShotApi moonShotApi) {
        this.moonShotApi = moonShotApi;
    }

    @RequestMapping("/getAnswer")
    public String chat(String question) throws IOException, ParseException {
        JSONObject jsonObject = moonShotApi.singleChat(question);
        JSONObject choice = (JSONObject) jsonObject.getJSONArray("choices").get(0);
        return choice.getJSONObject("message").getStr("content");
    }
}
